package com.warmlight.voicepacket.utils;

import android.content.Context;
import android.content.res.AssetManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class JSONUtils {

    /**
     * 从json中获取MAP数组
     *
     * @param json
     * @return
     */
    public static ArrayList<Map<String, String>> getListMapByJson(Object json) {
        ArrayList<Map<String, String>> objs = new ArrayList<Map<String, String>>();
        JSONArray array = new JSONArray();
        // 尝试解析
        try {
            if (json == null)
                return objs;
            else if (json.getClass() == JSONArray.class)
                array = (JSONArray) json;
            else if (((String) json).length() == 0)
                return objs;
            else
                array = new JSONArray((String) json);
        } catch (JSONException e1) {
            try {
                array.put(new JSONObject((String) json));
            } catch (JSONException e2) {
//                reportError("Json无法解析", null);
            }
        }
        for (int i = 0; i < array.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            try {
                Iterator<?> it = array.getJSONObject(i).keys();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    Object xx = array.getJSONObject(i).get(key);
                    map.put(key, xx.toString());
                }
            } catch (Exception e) {
                // 直接取数组值到map中,key为空
                try {
                    map.put("", array.get(i).toString());
                } catch (JSONException e1) {
//                    reportError("Json无法解析", null);
                }
            }
            objs.add(map);
        }
        return objs;
    }

    //本地json文件
    public static String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }



    public static String map2Json(Map<String,String> map){
        JSONObject jsonObject = new JSONObject();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, String> entry = it.next();
            try {
                String value = "";
                if(entry.getValue() != null && !entry.getValue().equals("null"))
                    value = entry.getValue();
                jsonObject.put(entry.getKey(),value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject.toString();
    }

    public static JSONObject map2JsonNoToString(Map<String,String> map){
        JSONObject jsonObject = new JSONObject();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, String> entry = it.next();
            try {
                String value = "";
                if(entry.getValue() != null && !entry.getValue().equals("null"))
                    value = entry.getValue();
                jsonObject.put(entry.getKey(),value);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

    public static String list2Json(List<Map<String,String>> data){
        //添加JS回调代码
        JSONArray jsonArray = new JSONArray();
        for(Map<String,String> map : data){
            JSONObject jsonObject = new JSONObject();
            try {
                Iterator<Map.Entry<String,String>> it = map.entrySet().iterator();
                while(it.hasNext()){
                    Map.Entry<String,String> entry = it.next();
                    jsonObject.put(entry.getKey(), entry.getValue());
                }
            } catch (JSONException e) {
//                UtilLog.reportError("JSON生成异常", e);
            }
            jsonArray.put(jsonObject);
        }
        return jsonArray.toString();
    }

    public static JSONArray list2JsonArray(List<Map<String,String>> data){
        //添加JS回调代码
        JSONArray jsonArray = new JSONArray();
        for(Map<String,String> map : data){
            JSONObject jsonObject = new JSONObject();
            try {
                Iterator<Map.Entry<String,String>> it = map.entrySet().iterator();
                while(it.hasNext()){
                    Map.Entry<String,String> entry = it.next();
                    jsonObject.put(entry.getKey(), entry.getValue());
                }
            } catch (JSONException e) {
//                UtilLog.reportError("JSON生成异常", e);
            }
            jsonArray.put(jsonObject);
        }
        return jsonArray;
    }


}
